package com.maidou.qiantai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.maidou.qiantai.dto.member.LoginDto;
import com.maidou.qiantai.dto.member.MemberInfoDto;
import com.maidou.qiantai.dto.member.PwdDto;
import com.maidou.qiantai.vo.member.LoginVo;
import com.maidou.qiantai.vo.member.MemberVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.Member;
import com.maidou.qiantai.service.IMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户Controller
 * 
 * @author maidou
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/qiantai/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 登录注册
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDto loginDto)
    {
        LoginVo vo = memberService.login(loginDto);
        return AjaxResult.success(vo);
    }

    /**
     * 用户基本信息
     * @return
     */
    @GetMapping("/info")
    public AjaxResult userInfo()
    {
        MemberVo vo = memberService.selectUserinfo();
        return AjaxResult.success(vo);
    }

    /**
     * 修改用户
     */
    @PostMapping("/edit-info")
    public AjaxResult edit(@RequestBody MemberInfoDto memberInfoDto)
    {
        return toAjax(memberService.updateMemberInfo(memberInfoDto));
    }

    /**
     * 修改密码
     */
    @PostMapping("/edit-pwd")
    public AjaxResult editPwd(@RequestBody PwdDto pwdDto)
    {
        return toAjax(memberService.updateMemberEditPwd(pwdDto));
    }

    /**
     * 修改头像
     */
    @PostMapping("/edit-Header")
    public AjaxResult editHeader(@RequestPart("file") MultipartFile multipartFile)
    {
        return AjaxResult.success(memberService.updateMemberEditHeader(multipartFile));
    }


    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:member:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(memberService.selectMemberById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('qiantai:member:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('qiantai:member:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberService.deleteMemberByIds(ids));
    }
}
